﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TfsUtils.Commands
{
    public class Parameter
    {
        public String Name { get; set; }

        public String Description { get; set; }

        public Object Default { get; set; }

        public Parameter(string name, string description) : this(name, description, null)
        {
        }

        public Parameter(string name, string description, object defaultValue)
        {
            Name = name;
            Description = description;
            Default = defaultValue;
        }



        public void Fill(Dictionary<string, object> parameterValues, string[] parameters)
        {
            object paramValue = GetParamValue(parameters);
            parameterValues.Add(Name, paramValue);
        }

        public object GetParamValue(string[] parameters)
        {
            String param = parameters.Where(
                p => p.StartsWith("/" + Name, StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
            object paramValue = Default;
            if (param != null)
           
            {
                int startIndex = param.IndexOf(":") + 1;
                string value = param.Substring(Math.Max(startIndex, 1));
                paramValue = value.Trim('\"');
                
            }
            return paramValue;
        }
    }
}
